//
//  AppDelegate.m
//  FamousDoctorOnline
//
//  Created by wszyxc on 16/7/5.
//  Copyright © 2016年 wszyxc. All rights reserved.
//

#import "AppDelegate.h"
#import "JASidePanelController.h"

#import "FDLeftController.h"
#import "FDHomeController.h"
#import "FDNavController.h"

#import "XZMCoreNewFeatureVC.h"
#import "CALayer+Transition.h"

@interface AppDelegate ()


//侧滑控制器
@property (strong,nonatomic) JASidePanelController * viewController;


@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    //MARK:设置网络监控
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(reachbilityChange:) name:kReachabilityChangedNotification object:nil];
    self.reach = [Reachability reachabilityForInternetConnection];
    
    self.reach=[Reachability reachabilityWithHostName:@"www.baidu.com"];
    
    //开启运行时通知
    [self.reach startNotifier];
    
    
    //MARK:设置窗口显示
    self.window = [[UIWindow alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT)];
    
    self.window.backgroundColor = [UIColor whiteColor];
    
    //实例化侧滑控制器
    self.viewController = [[JASidePanelController alloc]init];
    
    //左控制器
    self.viewController.leftPanel = [[FDLeftController alloc]init];
    
    FDHomeController *homeVC = [[FDHomeController alloc]init];
    
    //中间控制器
    self.viewController.centerPanel = [[UINavigationController alloc]initWithRootViewController:homeVC];
    
    
    //设置根控制器
//    self.window.rootViewController = self.viewController;
    
    //设置新特性界面
    [self newFeatures];
    
    
    //显示window
    [self.window makeKeyAndVisible];
    
    
    
    return YES;
}


-(void)newFeatures{
    //判断是否需要显示：（内部已经考虑版本及本地版本缓存）
    BOOL canShow = [XZMCoreNewFeatureVC canShowNewFeature];
    
    NSLog(@"%d",canShow);
    if(canShow){ // 初始化新特性界面
        self.window.rootViewController = [XZMCoreNewFeatureVC newFeatureVCWithImageNames:@[@"guid_iphone5_1",@"guid_iphone5_2",@"guid_iphone5_3"] enterBlock:^{
            
            [self enter:self.viewController];
            
        } configuration:^(UIButton *enterButton) { // 配置进入按钮
            
            enterButton.backgroundColor = [UIColor clearColor];
            enterButton.bounds = CGRectMake(0, 0, 104, 36);
            enterButton.center = CGPointMake(KScreenW * 0.5, KScreenH* 0.915);
        }];
        
    }else{
        
        [self enter:self.viewController];
    }
    
    [self.window makeKeyAndVisible];
}

// 进入主页面
-(void)enter:(JASidePanelController *)viewController{
    
    self.window.rootViewController = viewController;
    
}

-(void)reachbilityChange:(NSNotification *)nof{
    
    Reachability *rech=nof.object;
    
    if ([rech isKindOfClass:[Reachability class]]) {
        
        NetworkStatus status=[rech currentReachabilityStatus];
        
        self.isAchieve=YES;
        
        if (status==NotReachable) {
            
            self.isAchieve=NO;
            
            [self presentAlertVC:@"网络连接异常"];
            
        }else if(status ==ReachableViaWWAN){
            
            [self presentAlertVC:@"连接到移动网络"];
            
        }else if(status ==ReachableViaWiFi){
            
            [self presentAlertVC:@"连接上wify"];
            
        }
    }
}

-(void)presentAlertVC:(NSString *)str{
    
    //创建网络异常弹框
    UIAlertController *alertVC=[UIAlertController alertControllerWithTitle:str message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    
    //JASidePanelController *vc=(JASidePanelController *)self.window.rootViewController;
    
    UIAlertAction *identify=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *cancle=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    [alertVC addAction:cancle];
    [alertVC addAction:identify];
    
    if ([self.reach isKindOfClass:[Reachability class]]) {
        
        [self.viewController presentViewController:alertVC animated:YES completion:nil];
    }
}

-(void)dealloc{
    
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
